<?php
/*
	Muffinz CMS - A website content management system.
	Copyright (C) 2010-2011 Sigitas Zelenkovas

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/

if($exec == 1){
	if($_SESSION['user'] == null){
		$cTheme->AddModuleError("Login required.");
	}else if($_SESSION['user'] != null){
		$cTheme->SetTitle("Edit profile");
	
		if(isset($_POST['submitp'])){
			$buffer  = "";
			if($_POST['cpassn'] != $_POST['cpassnc']){
				$buffer .= "<li>New password and its confirmation does not match.</li>";
			}else if(!(strlen($_POST['cpassn']) >= 4 && strlen($_POST['cpassnc']) >= 4)){
				$buffer .= "<li>New password has to be atleast 4 characters long.</li>";
			}
			if(hash("sha512", $_POST['cpassCur']) != $_SESSION['user']['password']){
				$buffer .= "<li>Please enter valid old password.</li>";
			}
			
			if($buffer == ""){
				$q->Send("UPDATE users SET password=\"".hash("sha512", $_POST['cpassn'])."\" WHERE id={$_SESSION['user']['id']}");
				$_SESSION['user']['password'] = hash("sha512", $_POST['cpassn']);
				$cTheme->AddModuleNotification("Your password has been changed successfully.");
			}else {
				$cTheme->AddModuleError("<b>Passwors has not changed because:</b>".$buffer);
			}
		}
		if(isset($_POST['submita'])){
			$buffer = "";
			$ext = end(explode(".", $_FILES['avatar']['name']));
			
			if($_FILES['avatar']['size'] > $_SESSION['sys']['core_avatar_max_size']){
				$buffer .= "<li>Avatar cannot be bigger than {$_SESSION['sys']['core_avatar_max_size']}bytes (".format_filesize($_SESSION['sys']['core_avatar_max_size'])."), your avatar is ".format_filesize($_FILES['avatar']['size'])."</li>";
			}
			if(!in_array($_FILES['avatar']['type'], explode(",", $_SESSION['sys']['core_avatar_mime_types']))){
				$buffer .= "<li>Avatar mime type not supported.</li>";
			}
			if(!in_array($ext, explode(",", $_SESSION['sys']['core_avatar_types']))){
				$buffer .= "<li>Avatar extention is not supported.</li>";
			}
			$imageInfo = getimagesize($_FILES['avatar']['tmp_name']);
			if($imageInfo[0] > $_SESSION['sys']['core_avatar_size_x']){
				$buffer .= "<li>Avatar is too wide. (allowed width: {$_SESSION['sys']['core_avatar_size_x']}px)</li>";
			}
			if($imageInfo[1] > $_SESSION['sys']['core_avatar_size_y']){
				$buffer .= "<li>Avatar is too high. (allowed height: {$_SESSION['sys']['core_avatar_size_y']}px)</li>";
			}
			
			if($buffer == ""){
				if(@move_uploaded_file($_FILES['avatar']['tmp_name'], $_SESSION['sys']['core_avatar_dir'].$_FILES['avatar']['name'])){
					if(!is_dir($_SESSION['sys']['core_avatar_dir'].$_SESSION['user']['username']."/")){
						mkdir($_SESSION['sys']['core_avatar_dir'].$_SESSION['user']['username']."/", 0777);
					}
					if(!isset($_SESSION['user']['settings']['avatarid'])){
						$user->SetSetting("avatarid", 1);
						$user->SetSetting("avatar", "1.{$_SESSION['user']['username']}.$ext");
						rename($_SESSION['sys']['core_avatar_dir'].$_FILES['avatar']['name'], $_SESSION['sys']['core_avatar_dir'].$_SESSION['user']['username']."/1.{$_SESSION['user']['username']}.$ext");
					}else {
						$user->SetSetting("avatarid", ($_SESSION['user']['settings']['avatarid'] + 1));
						$user->SetSetting("avatar", ($_SESSION['user']['settings']['avatarid'] + 1).".{$_SESSION['user']['username']}.$ext");
						rename($_SESSION['sys']['core_avatar_dir'].$_FILES['avatar']['name'], $_SESSION['sys']['core_avatar_dir'].$_SESSION['user']['username']."/".($_SESSION['user']['settings']['avatarid'] + 1).".{$_SESSION['user']['username']}.$ext");
					}
					$user->SaveSettings();
					$cTheme->AddModuleNotification("Your avatar has been chnaged successfully.");
				}else {
					$cTheme->AddModuleError("Unable to change your avatar, unable to move avatar to specific difectory.<br />Please chmod:<li>images/userAvatars to 0777</li>");
				}
			}else {
				$cTheme->AddModuleError("<b>Avatar was not applied because of these reasons:</b>".$buffer);
			}
		}
		if(isset($_POST['submits'])){
			$user->SetSetting("aboutme", htmlentities($_POST['about']));
			$user->SetSetting("skype", htmlentities($_POST['skype']));
			$user->SetSetting("steam", htmlentities($_POST['steam']));
			$user->SetSetting("youtube", htmlentities($_POST['youtube']));
			
			$user->SaveSettings();
			$cTheme->AddModuleNotification("Your settings were updated.");
		}
		
		$buffer  = "<form method=\"post\" action=\"\"><table>";
		$buffer .= "	<tr><td align=\"right\">Username:</td><td>{$_SESSION['user']['username']}</td></tr>";
		$buffer .= "	<tr><td align=\"right\">About me:</td><td><textarea name=\"about\">".(isset($_SESSION['user']['settings']['aboutme'])?$_SESSION['user']['settings']['aboutme']:"")."</textarea></td></tr>";
		$buffer .= "	<tr><td align=\"right\">Skype:</td><td><input type=\"text\" name=\"skype\" value=\"".(isset($_SESSION['user']['settings']['skype'])?$_SESSION['user']['settings']['skype']:"")."\" /></td></tr>";
		$buffer .= "	<tr><td align=\"right\">Steam:</td><td><input type=\"text\" name=\"steam\" value=\"".(isset($_SESSION['user']['settings']['steam'])?$_SESSION['user']['settings']['steam']:"")."\" /></td></tr>";
		$buffer .= "	<tr><td align=\"right\">Youtube:</td><td><input type=\"text\" name=\"youtube\" value=\"".(isset($_SESSION['user']['settings']['youtube'])?$_SESSION['user']['settings']['youtube']:"")."\" /></td></tr>";
		$buffer .= "	<tr><td align=\"right\">&nbsp;</td><td><input type=\"submit\" name=\"submits\" value=\"Save changes\" class=\"button\" /></td></tr>";
		$buffer .= "</table></form>";
		$cTheme->AddModule("Edit profile", $buffer, 5);
		
		$buffer  = "<form method=\"post\" action=\"\" enctype=\"multipart/form-data\"><table>";
		$buffer .= "	<tr><td align=\"right\">Current avatar:</td><td>".$user->GetAvatar(true)."</td></tr>";
		$buffer .= "	<tr><td align=\"right\">Maximum avatar size:</td><td>Width: {$_SESSION['sys']['core_avatar_size_x']}px Height: {$_SESSION['sys']['core_avatar_size_y']}px</td></tr>";
		$buffer .= "	<tr><td align=\"right\">Alowed types:</td><td>".(str_replace(",", ", ", $_SESSION['sys']['core_avatar_types']))."</td></tr>";
		$buffer .= "	<tr><td align=\"right\">Select file:</td><td><input type=\"file\" name=\"avatar\" /></td></tr>";
		$buffer .= "	<tr><td align=\"right\">&nbsp;</td><td><input type=\"submit\" name=\"submita\" value=\"Change avatar\" class=\"button\" /></td></tr>";
		$buffer .= "</table></form>";
		$cTheme->AddModule("Change avatar", $buffer, 6);
		
		$buffer  = "<form method=\"post\" action=\"\"><table>";
		$buffer .= "	<tr><td align=\"right\">New password:</td><td><input type=\"password\" name=\"cpassn\" /></td></tr>";
		$buffer .= "	<tr><td align=\"right\">Confirm new password:</td><td><input type=\"password\" name=\"cpassnc\" /></td></tr>";
		$buffer .= "	<tr><td align=\"right\">Current password:</td><td><input type=\"password\" name=\"cpassCur\" /></td></tr>";
		$buffer .= "	<tr><td align=\"right\">&nbsp;</td><td><input type=\"submit\" name=\"submitp\" value=\"Change password\" class=\"button\" /></td></tr>";
		$buffer .= "</table></form>";
		$cTheme->AddModule("Change password", $buffer, 7); 
	}
}else { header("Location: /"); }
